package example1;

import java.util.concurrent.*;

// 线程创建方式3，实现callable接口
public class TestCallable implements Callable<Boolean> {
    private String url; // 网络图片的地址

    private String name;  // 保存的文件名

    public TestCallable(String url, String name) {
        this.url = url;
        this.name = name;
    }

    // 下载图片线程的执行体
    @Override
    public Boolean call() throws Exception {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载了文件名为：" + name);
        return true;
    }


    public static void main(String[] args) {

        // 创建目标对象
        TestCallable t1 = new
                TestCallable("https://i1.hdslb.com/bfs/archive/55a3c63cf52355f0b35179758fc4df74c8c29b75.jpg@412w_232h_1c.jpg", "1.jpg");
        TestCallable t2 = new
                TestCallable("https://i0.hdslb.com/bfs/feed-admin/e2566bc67196ba0f6cd633146d81eec476abcee3.jpg@412w_232h_1c", "2.jpg");
        TestCallable t3 = new
                TestCallable("https://i0.hdslb.com/bfs/feed-admin/c22765645393e853d4139cf23eb79ef5f689690b.jpg@320w_184h_1c_100q.jpg", "3.jpg");

        // 创建执行服务
        ExecutorService ser = Executors.newFixedThreadPool(3);

        //提交执行,类似于start
        Future<Boolean> result1 = ser.submit(t1);
        Future<Boolean> result2 = ser.submit(t2);
        Future<Boolean> result3 = ser.submit(t3);

//        获取结果
        try {
            boolean r1 = result1.get();
            boolean r2 = result2.get();
            boolean r3 = result3.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        ;

        ser.shutdownNow();
    }
}
